package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.domain.SysPrivilege;
import com.demo.mapper.SysPrivilegeMapper;
import com.demo.service.SysPrivilegeService;
import org.springframework.util.CollectionUtils;

@Service
public class SysPrivilegeServiceImpl extends ServiceImpl<SysPrivilegeMapper, SysPrivilege> implements SysPrivilegeService{

    @Autowired
    private SysPrivilegeMapper sysPrivilegeMapper;
    /**
     * 获取该菜单下所有权限
     * @param menuId
     * @param roleId
     * @return
     */
    @Override
    public List<SysPrivilege> getAllSysPrivilege(Long menuId,Long roleId) {
        //1.查询所有该菜单下的权限
        List<SysPrivilege> sysPrivilegeList = list(new LambdaQueryWrapper<SysPrivilege>().eq(SysPrivilege::getMenuId,menuId));
        if(CollectionUtils.isEmpty(sysPrivilegeList)){
            return Collections.emptyList();
        }
        //2.当前传递的角色使用包含 该权限信息也要放进去
        for (SysPrivilege sysPrivilege : sysPrivilegeList){
            Set<Long> currentRoleSysPrivilegeIds = sysPrivilegeMapper.geteSysPrivilegeByRoleId(roleId);
            if(currentRoleSysPrivilegeIds.contains(sysPrivilege.getId())){
                sysPrivilege.setOwn(1);
            }
        }
        return sysPrivilegeList;
    }
}
